package org.study.bookmanagerclientbyswing.model;

import javax.swing.table.AbstractTableModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class BookTableModel extends AbstractTableModel {
    private final String[] columnNames = {"ID", "书名", "作者", "出版年数", "出版社","可借数","库存总数","单价"};
    private final List<Map<String, Object>> books;

    public BookTableModel(List<Map<String, Object>> books) {
        this.books = books;
    }

    @Override
    public int getRowCount() {
        return books.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Map<String, Object> book = books.get(rowIndex);
        return switch (columnIndex) {
            case 0 -> book.get("bookId");
            case 1 -> book.get("title");
            case 2 -> book.get("author");
            case 3 -> book.get("publicationYear");
            case 4 -> book.get("publisher");
            case 5 -> book.get("availableToBorrow");
            case 6 -> book.get("stockQuantity");
            case 7 -> book.get("price");
            default -> null;
        };
    }

    @Override
    public String getColumnName(int columnIndex) {
        return columnNames[columnIndex];
    }
}